/**
 *Copyright (c) 2024 watereyes
 * workflow is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.workflow.api.service.impl;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Service;
import org.swan.bean.MessageBean;

import com.koron.workflow.api.bean.RelationBean;
import com.koron.workflow.api.bean.query.RelationQueryBean;
import com.koron.workflow.api.mapper.RelationMapper;
import com.koron.workflow.api.service.RelationService;
import com.koron.workflow.common.util.BusinessCode;
import com.koron.workflow.common.util.WorkflowUtil;

@Service
public class RelationServiceImpl implements RelationService {

	/**
	 * 添加节点关系
	 */
	@Override
	public MessageBean<?> addRelation(SessionFactory factory, int operation, RelationBean bean, String delegateTarget) {
		RelationMapper mapper = factory.getMapper(RelationMapper.class);
		RelationQueryBean query = null;
		if ("system_operation".equals(delegateTarget)) { // 此任务由系统自动完成
			query = new RelationQueryBean(bean.getTaskId(), bean.getRelation(), bean.getProcessInstanceId());
		} else {
			query = new RelationQueryBean(bean.getUserAccount(), bean.getTaskId(), bean.getRelation(), bean.getProcessInstanceId());
		}
		boolean exist = false;
		List<RelationBean> existing = mapper.findAll(query);
		if (null != existing && existing.size() > 0) {
			exist = true;
		}
		switch (operation) {
			case 2: { // 转办
				if (StringUtils.isEmpty(delegateTarget)) {
					return MessageBean.create(BusinessCode.FAILURE.getCode(), "转办人员不能为空", Void.class);
				}
				if (!exist) {
					return MessageBean.create(BusinessCode.FAILURE.getCode(), "委托人关系不存在", Void.class);
				}
				mapper.addRelation(new RelationBean(RelationBean.R_HANDLE, delegateTarget, bean.getProcessInstanceId(),
						existing.get(0).getTaskId(), RelationBean.STATUS_UNHANDLE));
				break;
			}
			case 4: { // 4 添加沟通人
				if (StringUtils.isEmpty(delegateTarget)) {
					return MessageBean.create(BusinessCode.FAILURE.getCode(), "请传入沟通人信息!", Void.class);
				}
				List<String> users = Arrays.asList(delegateTarget.split(","));
				for (String user : users) {
					RelationQueryBean rq = new RelationQueryBean(user, bean.getTaskId(), RelationBean.R_REPLY, bean.getProcessInstanceId());
					List<RelationBean> list = mapper.findAll(rq);
					if (0 == list.size()) {
						// 添加沟通人
						bean.setStatus(RelationBean.STATUS_UNHANDLE);
						bean.setRelation(RelationBean.R_REPLY);
						bean.setId(WorkflowUtil.getObjectId());
						bean.setUserAccount(user);
						mapper.addRelation(bean);
					}
				}
				break;
			}
			case 256: { // 256发起
				mapper.addRelation(bean);
				break;
			}
			case 512: { // 512添加候选人
				if (!exist) {
					mapper.addRelation(bean);
				}
				break;
			}
			case 1024: { // 1024 添加办理人（未办理）
				mapper.addRelation(bean);
				break;
			}
			default: {
				break;
			}
		}
		return MessageBean.create(BusinessCode.SUCCESS.getCode(), "执行成功", Void.class);
	}

	@Override
	public MessageBean<?> updateRelation(SessionFactory factory, int operation, RelationBean bean, String delegateTarget) {
		RelationMapper mapper = factory.getMapper(RelationMapper.class);
		RelationQueryBean query = new RelationQueryBean(bean.getUserAccount(), bean.getTaskId(), bean.getRelation(), bean.getProcessInstanceId());;
		boolean exist = false;
		List<RelationBean> existing = mapper.findAll(query);
		if (null != existing && existing.size() > 0) {
			exist = true;
		}
		switch (operation) {
			case 1: { // 驳回
				if (exist) {
					for (RelationBean one : existing) {
						one.setStatus(RelationBean.STATUS_HANDLED);
						mapper.updateRelation(one);
					}
				}
				break;
			}
			case 2: { // 转办
				if (exist) {
					for (RelationBean one : existing) {
						one.setStatus(RelationBean.STATUS_TRANS); // 驳回发起任务办理人 设为已认领
						mapper.updateRelation(one);
					}
				}
				break;
			}
			case 16: { // 16废弃
				List<RelationBean> list = mapper.findByProcessInstanceId(bean.getProcessInstanceId());
				for (RelationBean one : list) {
					if (one.getStatus() == RelationBean.STATUS_UNHANDLE) {
						one.setStatus(RelationBean.STATUS_HANDLED);
						mapper.updateRelation(one);
					}
				}
				break;
			}
			case 32: {// 32通过
				if (exist) {
					for (RelationBean relation : existing) {
						relation.setStatus(RelationBean.STATUS_HANDLED);
						mapper.updateRelation(relation);
					}
				}
				break;
			}
			case 64: {// 64 添加沟通/评论信息
				if (!exist) {
					return MessageBean.create(BusinessCode.FAILURE.getCode(), "该节点沟通人 " + bean.getUserAccount() + " 不存在!", Void.class);
				}
				for (RelationBean one : existing) {
					one.setStatus(RelationBean.STATUS_REPLY);
					mapper.updateRelation(one);
				}
				break;
			}
			case 128: {     // 128取消沟通人
				if (StringUtils.isEmpty(delegateTarget)) {
					return MessageBean.create(BusinessCode.FAILURE.getCode(), "取消沟通人操作请传入沟通人!", Void.class);
				}
				List<String> users = Arrays.asList(delegateTarget.split(","));
				for (String user : users) {
					RelationQueryBean query1 = new RelationQueryBean(user, bean.getTaskId(), bean.getRelation(), bean.getProcessInstanceId());
					List<RelationBean> relations = mapper.findAll(query1);
					for (RelationBean relation : relations) {
						mapper.deleteById(relation.getId()); // 删除沟通人
					}
				}
				break;
			}
			default: {
				break;
			}
		}
		return MessageBean.create(BusinessCode.SUCCESS.getCode(), "执行成功", Void.class);
	}

	@Override
	public MessageBean<?> deleteRelation(SessionFactory factory, String taskId, int relation, String userAccount) {
		int iCount = factory.getMapper(RelationMapper.class).deleteByTaskIdAndAccount(taskId, userAccount, relation);
		if(iCount > 0) {
			return MessageBean.create(BusinessCode.SUCCESS.getCode(), "删除成功", Void.class);
		}
		return MessageBean.create(BusinessCode.FAILURE.getCode(), "删除失败", Void.class);
	}

	@Override
	public List<RelationBean> listRelation(SessionFactory factory, RelationQueryBean query) {
		return factory.getMapper(RelationMapper.class).findAll(query);
	}

}
